<template>
  <div class="userInfo">
    <div class="box">
      <div class="left">
        <el-image :src="image" style="border-radius: 50%;width: 200px;height: 200px"/>
      </div>
      <div class="right">
        <el-form :model="userInfo" class="demo-form-inline">
          <el-form-item v-show="showStay" label="楼&#8195;栋:">
            <label>{{ userInfo.dormitoryName }}</label>
          </el-form-item>
          <el-form-item v-show="showStay" label="寝&#8195;室:">
            <label>{{ userInfo.number }}</label>
          </el-form-item>
          <el-form-item label="姓&#8195;名:">
            <label v-show="showInfo">{{ userInfo.name }}</label>
            <el-input v-show="!showInfo" v-model="userInfo.userName" placeholder="修改姓名"/>
          </el-form-item>
          <el-form-item label="电&#8195;话:">
            <label v-show="showInfo">{{ userInfo.phone }}</label>
            <el-input v-show="!showInfo" v-model="userInfo.phone" placeholder="修改电话"/>
          </el-form-item>
          <el-form-item label="邮&#8195;箱:">
            <label v-show="showInfo">{{ userInfo.eMail }}</label>
            <el-input v-show="!showInfo" v-model="userInfo.eMail" placeholder="修改邮箱"/>
          </el-form-item>
          <el-form-item label="身份证:">
            <label v-show="showInfo">{{ userInfo.cardId }}</label>
            <el-input v-show="!showInfo" v-model="userInfo.cardId" placeholder="修改身份证"/>
          </el-form-item>
          <el-form-item>
            <el-button v-show="showInfo" type="primary" @click="showInfos">
              <span>
                修改信息
              </span>
            </el-button>
            <el-button v-show="!showInfo" type="primary" @click="updateInfo">
              <span>
                确认修改
              </span>
            </el-button>
            <el-button type="danger" v-show="!showInfo" @click="closeInfo">取消修改</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="stay" v-show="showStay">
        <!-- <el-button type="primary" @click="changeStay">签到</el-button> -->
      </div>
    </div>
  </div>
</template>

<script>

import {getInfo, infoList} from "@/api/api";
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "userInfo",
  data() {
    return {
      image: require("@/assets/login.jpeg"),
      userInfo: {},
      showInfo: true,
      showStay: false,
      tableData: []
    }
  },
  methods: {
    getStayList() {
      let data = {}
      axios({
        url: "http://localhost:8888/stay/list",
        method: "post",
        data: data
      }).then(res => {
        console.log(res.data)
        if (res.status == 200) {
          this.tableData = res.data
        }
      })
    },
    changeStay() {
      let a = 0;
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.userInfo.id == this.tableData[i].userId) {
          axios({
            url: "http://localhost:8888/stay/delete/" + this.tableData[i].id,
            method: "delete"
          }).then(res => {
            if (res.data) {
              // ElMessage({
              //   message: "签到成功",
              //   type: "success"
              // })
              alert("签到成功")
              this.$router.go(0)
            } else {
              ElMessage({
                message: "签到失败",
                type: "danger"
              })
            }
          })
        } else {
          a = a + 1
        }
      }
      if (a == this.tableData.length) {
        ElMessage({
          message: "你已签到",
          type: "warning"
        })
      }
    },
    getImage() {
      let number = Math.round(Math.random() * 10);
      localStorage.setItem("image", number)
      this.image = require(`@/assets/${number}.jpeg`)
    },
    getImages(data) {
      this.image = require(`@/assets/${data}.jpeg`)
    },
    showInfos() {
      this.showInfo = false
    },
    closeInfo() {
      this.showInfo = true
    },
    updateInfo() {
      axios({
        url: `http://localhost:8888/info/updateInfo`,
        method: "put",
        data: this.userInfo
      }).then((res) => {
        if (res.data) {
          ElMessage({
            message: "修改信息成功,请重新登录",
            type: "success"
          })
          this.showInfo = true
          this.$router.push("/login")
        } else {
          ElMessage({
            message: "修改信息失败",
            type: "warring"
          })
        }
      })
    }
  }
  ,
  created() {
    axios({
      url: `http://localhost:8888/user/info`,
      method: "post",
      headers: {
        token: localStorage.getItem("token")
      }
    }).then((res) => {
      this.userInfo = res.data
      console.log("这是界面",this.userInfo)
      if (localStorage.getItem("image") == null) {
        this.getImage()
      } else {
        this.getImages(localStorage.getItem("image"))
      }
      if (this.userInfo.roleId == 3) {
        this.showStay = true
      } else {
        this.showStay = false
      }
    })
    this.getStayList()
  }
}
</script>

<style scoped lang="scss">

.userInfo {
  width: 100%;
  height: 100%;

  .box {
    margin: 2rem;
    display: flex;

    .left {
      width: 20%;
    }

    .right {
      width: 40%;

      ::v-deep .el-form-item__content {
        flex-wrap: nowrap;
      }
    }

    .stay {
      width: 20%;
    }

  }
}

</style>